﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;

namespace API.Controllers
{
    public class SearchListController : ApiController
    {
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());
        public HttpResponseMessage OptionsPatientDiagnosis()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

        public ReferralListResponse PostReferralList([FromBody]SearchListRequest request)
        {
            ReferralListResponse response = new ReferralListResponse();

            dsComm.AddParameter("@ParamLoginID", request.LoginID);
            dsComm.AddParameter("@SearchText", request.SearchText);

            string data = dsComm.ExecuteStoredProcedure("dbo.spGetSearchResultsForUser");
            XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
            DataSet ReferralDataSet = new DataSet();
            ReferralDataSet.ReadXml(xReader);

            if (ReferralDataSet.Tables.Count > 0)
            {
                response.ReferralList = ReferralDataSet.Tables[0];
                response.ErrorMessage = "";
            }
            else
            {
                response.ErrorMessage = "NoData";
            }

            return response;
        }
    }
}
